/**
 * JS naming-safe chars.
 */
const namingSafeChars =
  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$";

/**
 * convert number to short string based on naming-safe chars.
 * @param {number} num
 * @returns {string}
 */
export function shortName(num = 0) {
  let result = "";
  const size = namingSafeChars.length;
  while (num > 0) {
    result = namingSafeChars[num % size] + result;
    num = Math.floor(num / size);
  }
  return result;
}

const base64_chars =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/**
 * convert number to short string based on base64 chars
 * @param {number} num
 * @returns {string}
 */
export function short64(num = 0) {
  let result = "";
  while (num > 0) {
    result = base64_chars[num % 64] + result;
    num = Math.floor(num / 64);
  }
  return result;
}

/**
 * convert matrix to string
 * @param {any[][]} matrix
 * @returns {string}
 */
export function matrixToString(matrix, separators = [",", ";"]) {
  return matrix.map((line) => line.join(separators[0])).join(separators[1]);
}
